package com.bh.web.controller.extend;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.MapUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.extend.domain.JcReceivingParty;
import com.bh.extend.service.IJcReceivingPartyService;
import com.bh.extend.vo.JcCustomerReceivingPartyQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收发货地址Controller
 * 
 * @author slx
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/extend/receivingparty")
public class JcReceivingpartyController extends BaseController
{
    @Autowired
    private IJcReceivingPartyService jcReceivingpartyService;

    /**
     * 查询收发货地址列表,查询最近10条
     */
    @ApiOperation("查询收发货地址列表")
    @GetMapping("/list")
    public TableDataInfo list(JcReceivingParty jcReceivingparty)
    {
        startPage();
        List<JcReceivingParty> list = jcReceivingpartyService.selectJcReceivingpartyList(jcReceivingparty);
        return getDataTable(list);
    }


    /**
     * 查询收发货地址列表
     */
    @ApiOperation("查询客户收发货地址列表")
    @GetMapping("/listByCustomer")
    public AjaxResult listByCustomer(JcCustomerReceivingPartyQueryVo queryVo)
    {
        AjaxResult toAjaxResult = AjaxResult.success();
        JcReceivingParty sender = new JcReceivingParty();
        sender.setCustomerId(queryVo.getCustomerId());
        sender.setStatus(1);
        if (queryVo.getJcFromOrganizationId() != null) {
            sender.setJcOrganizationId(queryVo.getJcFromOrganizationId());
        }
        if (queryVo.getJcStartZoneId() != null) {
            sender.setJcZoneId(queryVo.getJcStartZoneId());
        }
        // 查询前10条
        startPage();
        List<JcReceivingParty> senderAddressList = jcReceivingpartyService.selectJcReceivingpartyList(sender);
        toAjaxResult.put("senderAddressList", senderAddressList);

        JcReceivingParty receiver = new JcReceivingParty();
        receiver.setCustomerId(queryVo.getCustomerId());
        if (queryVo.getJcToOrganizationId() != null) {
            receiver.setJcOrganizationId(queryVo.getJcToOrganizationId());
        }
        if (queryVo.getJcStartZoneId() != null) {
            receiver.setJcZoneId(queryVo.getJcEndZoneId());
        }
        startPage();
        List<JcReceivingParty> receiverAddressList = jcReceivingpartyService.selectJcReceivingpartyList(receiver);
        toAjaxResult.put("receiverAddressList", receiverAddressList);
        return toAjaxResult;
    }

    /**
     * 导出收发货地址列表
     */
    @PreAuthorize("@ss.hasPermi('extend:jcReceivingParty:export')")
    @Log(title = "收发货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcReceivingParty jcReceivingparty)
    {
        List<JcReceivingParty> list = jcReceivingpartyService.selectJcReceivingpartyList(jcReceivingparty);
        ExcelUtil<JcReceivingParty> util = new ExcelUtil<JcReceivingParty>(JcReceivingParty.class);
        util.exportExcel(response, list, "收发货地址数据");
    }

    /**
     * 获取收发货地址详细信息
     */
    @ApiOperation("获取收发货地址详细信息")
    @PreAuthorize("@ss.hasPermi('extend:jcReceivingParty:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(jcReceivingpartyService.selectJcReceivingpartyById(id));
    }

    /**
     * 新增收发货地址
     */
    @ApiOperation("新增收发货地址")
    @PreAuthorize("@ss.hasPermi('extend:jcReceivingParty:add')")
    @Log(title = "收发货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcReceivingParty jcReceivingparty)
    {
        jcReceivingparty.setJcZoneId(MapUtils.getAdcode(jcReceivingparty.getLtl()));
        jcReceivingparty.setJcOrganizationId(SecurityUtils.getOrgId());
        return toAjax(jcReceivingpartyService.insertJcReceivingparty(jcReceivingparty));
    }

    /**
     * 修改收发货地址
     */
    @ApiOperation("修改收发货地址")
    @PreAuthorize("@ss.hasPermi('extend:jcReceivingParty:edit')")
    @Log(title = "收发货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcReceivingParty jcReceivingparty)
    {
        if (jcReceivingparty.getJcZoneId() == null) {
            jcReceivingparty.setJcZoneId(MapUtils.getAdcode(jcReceivingparty.getLtl()));
        }
        return toAjax(jcReceivingpartyService.updateJcReceivingparty(jcReceivingparty));
    }

    /**
     * 删除收发货地址
     */
    @ApiOperation("删除收发货地址")
    @PreAuthorize("@ss.hasPermi('extend:jcReceivingParty:remove')")
    @Log(title = "收发货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jcReceivingpartyService.deleteJcReceivingpartyByIds(ids));
    }
}
